package com.xiaowu.datajpa.service;
import com.xiaowu.datajpa.constant.ResultEnum;
import com.xiaowu.datajpa.dao.StudentRepository;
import com.xiaowu.datajpa.entity.Student;
import com.xiaowu.datajpa.exception.ResultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentSercvice {
    @Autowired
    private StudentRepository studentRepository;

    /**
     * 查询所有学生
     * @return
     */
    public List<Student> findAll(){
        return studentRepository.findAll();
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    public Student findById(Integer id){
        return studentRepository.findById(id).orElse(null);
    }

    /**
     * 根据学号查询学生
     * @param stunumber
     * @return
     */
    public Student findByStunumber(String stunumber){
        return studentRepository.findByStunumber(stunumber);
    }
    /**
     * 根据名称查询学生
     * @param name
     * @return
     */
    public List<Student> findByNameLike(String name){
        return studentRepository.findByNameLike(name);
    }

    /**
     * 根据名称和密码查询学生
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameAndPassword(String name,String password){
        return studentRepository.findByNameAndPassword(name,password);
    }

    /**
     * 新增学生
     * @param student
     */
    public Student insert(Student student){
        return studentRepository.saveAndFlush(student);
    }

    /**
     * 修改学生
     * @param student
     */
    public Student update(Student student){
       return studentRepository.saveAndFlush(student);
    }

    /**
     * 删除学生
     * @param ids
     */
    public void delete(List<Integer> ids){
    	studentRepository.deleteAllByIdInBatch(ids);
    }

    /**
     * 根据学号和密码查询学生
     * @param stunumber
     * @param password
     * @return
     * @throws Exception
     */
    public Student findByStumberAndPassword(String stunumber, String password) throws Exception{
        Student student = findByStunumber(stunumber);
        if(student!=null){
            if(student.getPassword().equals(password)){
                return student;
            }else {
                throw new ResultException(ResultEnum.LOGIN_ERR);
            }
        }else {
            throw new ResultException(ResultEnum.LOGIN_ERR);
        }
    };

    /**
     * 分页查询所有学生
     * @param pageable
     * @return
     */
    public Page<Student> getAll(Pageable pageable){
        return studentRepository.findAll(pageable);
    }

    /**
     * 分页条件查询所有学生
     * @param example
     * @param pageable
     * @return
     */
    public Page<Student> getAll(Example<Student> example, Pageable pageable){
        return studentRepository.findAll(example,pageable);
    }

    @Transactional
    public void resetPassword(List<Integer> ids) {
        String newPassword = "123456";
        //根据ids将密码修改为123456
        studentRepository.updatePassword(newPassword, ids);
    }
}
